package com.mbatis.leeCode;

import org.junit.Test;

/**
 * @Classname RemoveDuplicates
 * @Date 2021/6/4 10:23
 * @Created by zhang.yuming
 */
public class RemoveDuplicates {

    @Test
    public void rd() {
        int[] nums = {0, 0, 1, 1, 1, 2, 2, 3, 3, 4};
        int ns = nums.length;
        for (int i = 0; i < nums.length; i++) {
            int num = nums[i];
            int slow = 0;
            for (int j = 1; j < ns; j++) {
                if (num != nums[j]) {
//                    System.out.println("nums[j]"+nums[j]);
                    slow++;
                }
            }
        }
    }


    public static void main(String args[]) {
        int[] nums = {0, 0, 1, 1, 1, 2, 2, 3, 3, 4};
//        int rd = removedup(nums);
//        System.out.println("rd=" + rd);
        // 移除元素与val相同的元素
        int val = 1;
//        int re1 = removeElement1(nums, val);
//        System.out.println(re1);

        int re2 = removeElement2(nums, val);
        System.out.println(re2);

    }

    // 移除元素  方式一
    private static int removeElement1(int[] nums, int val) {
        if (nums.length == 0) {
            return 0;
        }
        int n = nums.length;
        int ni = 0;
        for (int i = 0; i < n; i++) {
            if (nums[i] != val) {
                nums[ni] = nums[i];
                ni++;
            }
        }
        return ni;
    }


    private static int removeElement2(int[] nums, int val) {
        if (nums.length == 0) {
            return 0;
        }
        int numsLength = nums.length;
        int left = 0;
        while (left < numsLength) {
            if (val == nums[left]) {
                nums[left] = nums[numsLength - 1];
                numsLength--;
            } else {
                left++;
            }
        }
        System.out.println("nums:"+nums);
        return left;
    }


    // 移除元素的重复项
    private static int removedup(int[] nums) {
        int n = nums.length;
        if (n == 0) {
            return 0;
        }
        int fast = 1, slow = 1;
        while (fast < n) {
            if (nums[fast] != nums[fast - 1]) {
                nums[slow] = nums[fast];
                ++slow;
            }
            ++fast;
        }
        return slow;
    }


}
